// 题型
const QueryType = require("../../model/question/questiontype")

// 根据id获取题型
exports.getById = async ({ id }) => {
    try {
        if (!id) return { msg: "获取失败 id is undefind", code: "400" }
        let res = await QueryType.findOne({ where: { id } })
        if (res) return { msg: "获取成功", code: "200" }
        return { msg: "获取失败", code: "400" }
    } catch (err) { return { msg: "获取失败", code: "400" } }
}

// 获取题型列表
exports.getList = async ({ pageNum, pageSize }) => {
    try {
        pageNum = pageNum.replace(/\s*/g, "");
        pageSize = pageSize.replace(/\s*/g, "");
        if (!pageNum) return { msg: "获取失败 pageNum is undefind", code: "400" }
        if (!pageSize) return { msg: "获取失败 pageSize is undefind", code: "400" }
        pageNum = Number(pageNum) || 1
        pageSize = Number(pageSize) || 10
        if (!isNaN(pageNum) && !isNaN(pageSize)) {
            let res = await QueryType.findAndCountAll({
                offset: pageNum * pageSize - pageSize,
                limit: pageSize
            })
            console.log(res,"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
            return {msg:"获取成功", code:"200", data:res}
        } else {
            return { msg: "获取失败 pageNum or pageSize is undefind", code: "400" }
        }
    } catch (err) { return { msg: "获取失败", code: "400", err } }
}

// 添加题型
exports.create = async ({ title }) => {
    try {
        if (!title) return { msg: "添加失败 title is undefind", code: "400" }
        let res = await QueryType.create({ title })
        if (res) return { msg: "添加成功", code: "200" }
        return { msg: "添加失败", code: "400" }
    } catch (err) { return { msg: "添加失败", code: "400", err } }
}

// 编辑题型
exports.edit = ({ id }) => {

}

// 删除题型
exports.remove = ({ id }) => {

}
